package NG::Chat::Daemon;
use strict;
use warnings;
use utf8;

our $DEBUG;

use base 'NG::Sys::Loop::EVDaemon';

use EV;

use NG::Chat::Http;
use NG::Chat::Sock;
require NG::Chat::Chanel;
require NG::Chat::Login;
require NG::Chat::Command;

use NG::Sys::Cfg;

sub on_prepare_loop {
	my ($app) = @_;
	if($app->{sock_debug}){
		$NG::Sys::Sock::DEBUG =
		$NG::Chat::Sock::DEBUG =
			$app->logger($app->{sock_debug});
	}
	$app->{'chat-login'} = NG::Chat::Login::Create($app);
	$app->{'chat-command'} = NG::Chat::Command::Create($app);
	$app->{'chat-chanel'} = NG::Chat::Chanel::Create($app);
	
	my $process = $app->{'chat-command'}{process};

	if($app->{'http-addr'} && $app->{'http-port'}){
		$app->{'http-waiter'} = NG::Chat::Http::CreatePrepared($app->{'http-addr'},$app->{'http-port'},$process);
	}else{
		die "no http params\n";
	}
	if($app->{'sock-addr'} && $app->{'sock-port'}){
		$app->{'sock-waiter'} = NG::Chat::Sock::CreatePrepared($app->{'sock-addr'},$app->{'sock-port'},$process);
	}else{
		die "no sock params\n";
	}
}
sub on_after_loop {
	my ($app) = @_;
	delete $app->{'http-waiter'};
	delete $app->{'sock-waiter'};
	(delete $app->{'chat-login'})->cleanup;
	(delete $app->{'chat-chanel'})->cleanup;
	(delete $app->{'chat-command'})->cleanup;
	$app->SUPER::on_after_loop;
}

1;
